package com.yilife.hmstr.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yilife.hmstr.domain.FiveJudgmentSystem;
import com.yilife.hmstr.domain.MeridianData;
import com.yilife.hmstr.domain.MeridianTable;
import com.yilife.hmstr.domain.mt;
import com.yilife.hmstr.mapper.FiveJudgmentSystemMapper;
import com.yilife.hmstr.mapper.MeridianDataMapper;
import com.yilife.hmstr.mapper.MeridianTableMapper;
import com.yilife.hmstr.service.MeridianTableService;

@Service
public class MeridianTableServiceImpl implements MeridianTableService{
	
	@Autowired
	private MeridianTableMapper meridianTableMapper;
	
	@Autowired
	private FiveJudgmentSystemMapper fiveJudgmentSystemMapper;
	
	@Autowired
	private MeridianDataMapper meridianDataMapper;

	public void add(MeridianTable mt,List<FiveJudgmentSystem> fjs,List<MeridianData> md){
//		if(meridianTableMapper.selectById(mt.getHmstrId())==null){
		if(mt!=null&&selectByPid(mt.getHmstrId()).getMeridianTable()==null){
			meridianTableMapper.insert(mt);
			for (MeridianData meridianData : md) {
				meridianDataMapper.insert(meridianData);
			}
			for (FiveJudgmentSystem fiveJudgmentSystem : fjs) {
				fiveJudgmentSystemMapper.insert(fiveJudgmentSystem);
			}
		}else{}
	}
	
	public void del(long id){
		meridianDataMapper.delete(new EntityWrapper<MeridianData>().eq("mt_id", id));
		fiveJudgmentSystemMapper.delete(new EntityWrapper<FiveJudgmentSystem>().eq("mt_id", id));
		meridianTableMapper.deleteById(id);
	}
	
	/**
	 * 根据外键删除数据
	 * @param id
	 */
	public void delByPid(long id){
		List<MeridianTable> meridianTables=new MeridianTable().selectList(new EntityWrapper<MeridianTable>().eq("hmstr_id", id));
		for (MeridianTable meridianTable : meridianTables) {
			del(meridianTable.getMtId());
		}
	}
	
	public void update(){
		
	}
	/*
	public String selectById(int id){
		String str="";
		List<MeridianTable> meridianTables=new MeridianTable().selectList(new EntityWrapper<MeridianTable>().eq("mt_id", id));
		List<MeridianData> meridianDatas=new MeridianData().selectList(new EntityWrapper<MeridianData>().eq("mt_id", id));
		List<FiveJudgmentSystem> fjs=new FiveJudgmentSystem().selectList(new EntityWrapper<FiveJudgmentSystem>().eq("mt_id", id));
		str=str+meridianTables.toString();
		for (MeridianData meridianData : meridianDatas) {
			str=str+"\n\n"+meridianData.toString();
		}
		for (FiveJudgmentSystem fiveJudgmentSystem : fjs) {
			str=str+"\n\n"+fiveJudgmentSystem.toString();
		}
		return str;
	}
	
	
	public String selectByPid(int pid){
		String string="";
		List<MeridianTable> mts=meridianTableMapper.selectList(new EntityWrapper<MeridianTable>().eq("hmstr_id", pid));
		for (MeridianTable meridianTable : mts) {
			string=string+"\n\n"+selectById(meridianTable.getMtId());
		}
		return string;
	}
	*/
	public mt selectById(long id){
		List<MeridianTable> meridianTables=new MeridianTable().selectList(new EntityWrapper<MeridianTable>().eq("mt_id", id));
		List<MeridianData> meridianDatas=new MeridianData().selectList(new EntityWrapper<MeridianData>().eq("mt_id", id));
		List<FiveJudgmentSystem> fjs=new FiveJudgmentSystem().selectList(new EntityWrapper<FiveJudgmentSystem>().eq("mt_id", id));
		
		return new mt(meridianTables.size()==0?null:meridianTables.get(0),fjs.size()==0?null:fjs, meridianDatas.size()==0?null:meridianDatas);
	}
	
	public mt selectByPid(long pid){
		List<MeridianTable> mts=meridianTableMapper.selectList(new EntityWrapper<MeridianTable>().eq("hmstr_id", pid));
		
		return selectById(mts.size()==0?0:mts.get(0).getMtId());
	}
	
}
